Skip to content

bin/orphaned-pr-environments: Handle no PR envs and merged PRs better#889

Merged
doshitan merged 3 commits intomainfrom
doshitan/fix-orphaned-prs
Mar 4, 2025
Merged

bin/orphaned-pr-environments: Handle no PR envs and merged PRs better#889
doshitan merged 3 commits intomainfrom
doshitan/fix-orphaned-prs

Conversation

@doshitan
Copy link
Copy Markdown
Contributor

@doshitan doshitan commented Mar 3, 2025

Changes

When there are no PR environments for an application, grep 'pr-' against the
workspace names will not match anything, in which case grep returns a code of
1. Since -o pipefail is set, grep returning 1 will fail the pipeline,
and since -e is set, the pipeline will fail the script. So catch a return of
1 from grep and continue on.

Also handle merged PR statuses in addition to closed, as that will sometimes be
the case.

Also set a default for GITHUB_STEP_SUMMARY for when running the script
locally.

Testing

Before

Errors out when there are no PR envs.

image

After

Does not error out.

image

Before

Doesn't flag dangling PR environment from a merged PR.

image

After

image

Copy link
Copy Markdown
Contributor

@lorenyu lorenyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a question on the advantage of test $? = 1 over || true but I trust your expertise on bash over mine so gonna approve. I also had a question regarding the MERGED status, I have never actually seen that status

echo terraform -chdir="infra/${app_name}/service" workspace list
workspaces="$(terraform -chdir="infra/${app_name}/service" workspace list)"
pr_nums="$(echo "${workspaces}" | grep -o 'p-[0-9]\+' | sed 's/p-//')"
pr_nums="$(echo "${workspaces}" | { grep -o 'p-[0-9]\+' || test $? = 1; } | sed 's/p-//')"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we add a comment like

# Search for workspaces that start with "p-" and extract the PR numbers
# The "test $? = 1" is used to avoid errors if there are no PR environments

also, can you help me understand the advantage of test $? = 1 over || true? The latter seems simpler to grok.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

|| true ignores all errors, which while unlikely seems worse than just ignoring the specific expected case. grep will exit with 2 if something else is wrong.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok that makes sense, thanks

@doshitan doshitan marked this pull request as ready for review March 3, 2025 19:58
@doshitan doshitan merged commit 9aa2afb into main Mar 4, 2025
9 checks passed
@doshitan doshitan deleted the doshitan/fix-orphaned-prs branch March 4, 2025 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants